package com.example.test.demos.utils; // 确保这与您的目录结构匹配，且拼写正确

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class GameRedisUtil {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    // 添加或更新一个字符串值，带有过期时间
    public void setString(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    // 获取一个字符串值
    public String getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除一个字符串值
    public Boolean delString(String key) {
        return redisTemplate.delete(key);
    }

    // 更新一个字符串值（如果存在）
    public boolean updateString(String key, String value) {
        if (Boolean.TRUE.equals(redisTemplate.hasKey(key))) {
            redisTemplate.opsForValue().set(key, value);
            return true;
        }
        return false;
    }
}

